
#include "CondF.h"
#include "Common/TableDataEx/DuplicateCfg.h"


CondF::CondF() :m_pDuplicate(nullptr), m_pVecCond(nullptr), m_result(-1), m_finish(false), m_transArea(0), m_transNum(0)
{
	m_mapBossTrans.clear();
	m_setType.clear();
	m_targetNum = 0;
}

CondF::~CondF()
{

}

bool CondF::init(IDuplicate *pDuplicate, const VEC_INT32 *pCond, const string &strSubCond)
{
	if (nullptr == pDuplicate || nullptr == pCond)
	{
		return false;
	}
	//6，传送区域ID，目标值，BOSS1的ID，BOSS2的ID
	//条件6中，目标值必须填写；但可能没有BOSS怪物的ID，即全是小怪
	int32_t isize = pCond->size();
	if (isize < 3)
	{
		LogErrFmtPrint("[logic] CondF::init.....cond param size error....dupid:%u, sceneid:%u ,isize:%d ", pDuplicate->GetDuplicateID(), pDuplicate->GetSceneID(), isize);
		return false;
	}

	m_pVecCond = pCond;
	m_pDuplicate = pDuplicate;
	//传送区域ID
	m_transArea = m_pVecCond->at(1);
	//目标值
	m_targetNum = m_pVecCond->at(2);
	//
	for (int32_t i = 3; i < isize; ++i)
	{
		m_mapBossTrans[m_pVecCond->at(i)] = 0;
	}

	//解析怪物序列
	CommonApi::SplitStrToSetInt(strSubCond, ",", &m_setType);
	//移除类型为0的
	SET_INT32::iterator iterdel = m_setType.find(0);
	if (iterdel != m_setType.end())
	{
		m_setType.erase(iterdel);
	}

	return true;
}

void CondF::uninit()
{
	m_pDuplicate = nullptr;
	m_pVecCond = nullptr;
	m_finish = false;
	m_result = -1;
	m_transNum = 0;
	m_transArea = 0;
	m_targetNum = 0;
	m_mapBossTrans.clear();
	m_setType.clear();
}

bool CondF::finishCond()
{
	return m_finish;
}

int32_t CondF::result()
{
	return m_result;
}

uint32_t CondF::transAreeId()
{
	return m_transArea;
}

void CondF::onTransNum(uint32_t monsId, uint32_t num,int8_t parentType)
{
	MAP_UINT32_INT32::iterator iter = m_mapBossTrans.find(monsId);
	if (iter != m_mapBossTrans.end())
	{
		iter->second += (int32_t)num;
	}
	else if (0 == m_setType.size())
	{
		m_transNum += num;
		notifyTransNum();
	}
	else
	{
		SET_INT32::iterator iterType = m_setType.find(parentType);
		if (iterType != m_setType.end())
		{
			m_transNum += num;
			notifyTransNum();
		}
	}
	
}

void CondF::notifyTransNum()
{
	if (nullptr == m_pDuplicate)
	{
		return;
	}
	Duplicate_TransMonsNumRsp rsp;
	rsp.set_trans_num(m_transNum);
	m_pDuplicate->BroadCastData(EMODULE_ID_DUPLICATE, LOGIC_TO_CLIENT_DUPLICATE_ALREDY_TRANS, &rsp);
}

void CondF::condUpdate()
{
	if (m_finish)
	{
		return;
	}
	m_result = checkCond();
	if (RET_SUCCESS == m_result || RET_FAIL == m_result)
	{
		m_finish = true;
	}
}

int32_t CondF::checkCond()
{
	if (nullptr == m_pVecCond || nullptr == m_pDuplicate)
	{
		return -1;
	}
	/*
	1，指定时间内消灭指定怪物序列，才能通关；超时失败
	2，指定时间内消灭所有怪物，才能通关；超时失败
	3，消灭所有怪物，才能通关；塔被怪物消灭失败
	4，达到指定时间点，怪物传送的数量小于目标值；怪物传送的数量达到目标值，或有BOSS进行传送，失败
	6，消灭场景内所有还存在的脚本刷新怪物，胜利；	脚本刷新怪物的传送数量达到目标值，或有脚本刷新的BOSS进行传送，失败


	1，限制时间，怪物1的ID，怪物2的ID，……
	2，限制时间
	3，塔的ID
	4，限制时间，传送区域ID，目标值，BOSS1的ID，BOSS2的ID，……
	6，传送区域ID，目标值，BOSS1的ID，BOSS2的ID
	*/
	int32_t ret = -1;
	bool hasBossTrans = false;
	MAP_UINT32_INT32::iterator iter = m_mapBossTrans.begin();
	for (; iter != m_mapBossTrans.end(); ++iter)
	{
		if (iter->second > 0)
		{
			hasBossTrans = true;
			break;
		}
	}

	if (hasBossTrans) //有boss被传送，失败
	{
		ret = RET_FAIL;
	}
	else if (m_transNum >= m_targetNum) //传送达到目标值，失败
	{
		ret = RET_FAIL;
	}
	else if (m_pDuplicate->IsBrushMonsAllDie()) //消灭场景内所有还存在的脚本刷新怪物，胜利；
	{
		ret = RET_SUCCESS;
	}

	return ret;
}
